<#import "../tpl/pageTep.ftl" as page>
<@page.pageBase currentMenu="礼券管理">
<style>
    .state{
        cursor: pointer;
    }
</style>
<form id="searchForm" class="form-panel" action="${basepath}/rest/manage/voucher/selectVoucherList">
    <ul class="panel-content">
        <li>
            <div>
                <div class="control-group span5">
                    <div class="search-controls  controls">
                        <input type="text" name="name" id="name" value="" placeholder="搜索礼券名称、礼券类型">
                    </div>
                </div>
                <div class="form-actions span2">
                    <button type="submit" class="button  button-primary">搜索</button>
                </div>
            </div>
            <div class="form-actions">
                <a href="javascript: addVoucher();" class="button button-success">
                    <i class="icon-plus-sign icon-white"></i> 派送礼券
                </a>
            </div>
        </li>
    </ul>
</form>
<div id="grid"></div>
<#--派送礼券-->
<div id="editContent" style="display:none;">
    <form id="editForm" class="form-horizontal">
        <div class="row">
            <div class="control-group span16">
                <div class="controls">
                    <label class="control-label"><s>*</s>选择礼券：</label>
                    <div id="select" style="float: left;">
                        <input type="hidden" class="input-normal " name="storeID" value="" id="p-value"
                               data-rules="{required : true}">
                    </div>
                </div>
            </div>
            <div class="control-group span16">
                <div class="controls">
                    <label class="control-label"><s>*</s>派送用户：</label>
                    <div id="objselect" style="float: left;">
                        <input type="hidden" class="input-normal obj" name="storeID" value="" id="objvalue"
                               data-rules="{required : true}">
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>
<script>
    var Select = BUI.Select;
    var Grid = BUI.Grid,
            Store = BUI.Data.Store,
            columns = [
                {title: '序号', dataIndex: 'id', elCls: 'center', width: '20%'},
                {title: '派送记录时间', dataIndex: 'createTime', elCls: 'center', width: '20%'},
                {title: '礼券名称', dataIndex: 'voucherName', elCls: 'center', width: '20%'},
                {
                    title: '指派角色', dataIndex: 'roleID', elCls: 'center', width: '20%', renderer: function (value) {
                        switch (value) {
                            case  '1':
                                return '超级管理员';
                                break;
                            case  '2':
                                return '股东';
                                break;
                            case  '3':
                                return '会员';
                                break;
                            default :
                                return '普通用户';
                                break;
                        }
                    }
                },
                {
                    title: '礼券状态', dataIndex: 'status', elCls: 'center', width: '20%', renderer: function (value,data) {
                        var state=value=='y'?'启用':'禁用';
                        return  '<a class="state" data-state="'+value+'" data-id="'+data.id+'">'+state+'</a>';
                    }
                },
                {
                    title: '操作', dataIndex: 'id', elCls: 'center', width: '20%',
                    renderer: function (value, obj, index) {
                        return '<a  href="javascript: deleteFunction(' + value + ');">删除</a>';
                    }
                }
            ];
    var id = getQueryString('id');
    var store = new Store({
                url: '/rest/manage/voucher/selectVoucherSendList?voucherID=' + id,
                autoLoad: true, //自动加载数据
                params: { //配置初始请求的参数
                    length: '10',
                    status: $("#status").val()
                },
                pageSize: 10,	// 配置分页数目
                root: 'data',
                totalProperty: 'total'
            }),
            grid = new Grid.Grid({
                render: '#grid',
                columns: columns,
                loadMask: true, //加载数据时显示屏蔽层
                store: store,
                plugins: [Grid.Plugins.CheckSelection], // 插件形式引入多选表格
                // 底部工具栏
                bbar: {
                    pagingBar: true
                }
            });

    grid.render();
    var editForm = new BUI.Form.Form({
        srcNode: '#editForm',
        submitType: 'ajax',
        callback: function (data) {

        }
    });
    editForm.render();
    var editDialog = new BUI.Overlay.Dialog({
        width: 400,
        contentId: 'editContent',
        success: function () {
            if (editForm.isValid()) {
                var roleID = $('.obj').attr('value');
                $.ajax({
                    type: "POST",
                    url: "/rest/manage/vouchersend/sendVoucher",
                    dataType: "json",
                    data: {
                        voucherID: id,
                        roleID: roleID
                    },
                    success: function (data) {
                        var obj = new Object();
                        obj.start = 0; //返回第一页
                        store.load(obj);
                        editDialog.close();
                    }
                });
            } else {
                editForm.valid();
            }
        }
    });
    //修改状态
    function changeStatus(status,id){
        $.ajax({
            url:'/rest/manage/vouchersend/updateJson',
            type:'POST',
            dataType:'json',
            data:{
                status:status,
                id:id
            },
            success:function () {
                var obj=new Object();
                obj.start=0;
                store.load(obj);
            }
        })
    }
    $(document).on('click','.state',function () {
        var state=$(this).attr('data-state');
        var id=$(this).attr('data-id');
        state=state=='y'?'n':'y';
        changeStatus(state,id)
    });
    //查询入股店铺
    function addVoucher() {
        var selections = grid.getSelection();
        var ids = new Array();
        for (var i = 0; i < selections.length; i++) {
            ids[i] = selections[i].id.toString()
        }
        editDialog.show();
    }

    //单个删除操作
    function deleteFunction(value) {
        BUI.Message.Confirm('确认要删除吗？', function () {
            $.ajax({
                type: "POST",
                url: "${basepath}/rest/manage/vouchersend/deleteJson",
                dataType: "json",
                data: {
                    id: value
                },
                success: function (data) {
                    var obj = new Object();
                    obj.start = 0; //返回第一页
                    store.load(obj);
                }
            });
        }, 'question');
    }

    //模糊查询
    var form = new BUI.Form.HForm({
        srcNode: '#searchForm'
    }).render();

    form.on('beforesubmit', function (ev) {
        //序列化成对象
        var obj = form.serializeToObject();
        obj.start = 0; //返回第一页
        store.load(obj);
        return false;
    });
    //查询礼券
    var pStore = new Store({
        url: '/rest/manage/voucher/selectAllList',
        autoLoad: true,
        root: 'data'
    });
    var sSelect;
    sSelect = new Select.Select({
        render: '#select',
        valueField: '#p-value',
        list: {
            itemTpl: '<li >{name}</li>',
            idField: 'id'
        },
        store: pStore
    }).render();
    //查询用户
    var oStore = new Store({
        url: '/rest/manage/role/selectAllList',
        autoLoad: true,
        root: 'data'
    });
    var oSelect;
    oSelect = new Select.Select({
        render: '#objselect',
        valueField: '#objvalue',
        list: {
            itemTpl: '<li >{role_name}</li>',
            idField: 'id'
        },
        store: oStore
    }).render();
</script>

</@page.pageBase>
